<?php


namespace app\user\controller;


use think\Controller;

use think\facade\Request;
use think\facade\Session;
use app\user\model\Select as selectmodel;
use app\user\model\Student as studentmodel;
use app\user\model\Message as messagemodel;
use app\user\model\Notice as noticemodel;
use app\user\model\Order as ordermodel;
use app\user\model\Expert as expertmodel;
use app\user\model\Notify as notifymodel;
use app\user\model\Ucpaas;

class Student extends Controller
{

    //检测session
    protected function logincheck()
    {
        if (Session::get('studentname')) {
            return true;
        } else {
            return false;
        }
    }

    //盐值加密函数
    public function salt($pwd)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $salt  = "ssjxyz";  //定义一个salt值，程序员规定下来的随机字符串
            $spwd  = $pwd . $salt;  //把密码和salt连接
            $mspwd = md5($spwd);  //执行MD5散列
            return $mspwd;  //返回散列
        }
    }

    public function index()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("student/index");
        }

    }

    public function notice()
    {
        $res = noticemodel::select();
        $this->assign([
            'notice' => $res,
        ]);
        return $this->fetch("student/notice");
    }

    public function message()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where1['receive'] = Session::get('studentname');
            $where1['sstatus'] = 1;
            $slist             = notifymodel::where($where1)->select();
            $ccount            = sizeof($slist);
            $expert            = expertmodel::field('name')->select();
            $this->assign([
                'count'  => $ccount,
                'expert' => $expert
            ]);
            return $this->fetch("student/message");
        }

    }

    //我的预约
    public function myorder()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['sname']  = Session::get('studentname');
            $where['status'] = 1;
            $list            = ordermodel::where($where)->select();
            $count           = sizeof($list);
            $list            = selectmodel::field('sex')->select();
            $list2           = selectmodel::field('depart')->select();
            $list3           = selectmodel::field('slevel')->select();
            $list4           = expertmodel::field('name')->select();
            $list5           = selectmodel::field('roomnum')->select();

            $where1['receive'] = Session::get('studentname');
            $where1['sstatus'] = 1;
            $slist             = notifymodel::where($where1)->select();
            $ccount            = sizeof($slist);

            $this->assign([
                'count'       => $count,
                'sexlist'     => $list,
                'departlist'  => $list2,
                'slevellist'  => $list3,
                'enamelist'   => $list4,
                'roomnumlist' => $list5,
                'xcount'      => $ccount
            ]);
            return $this->fetch("student/myorder");
        }
    }


    //已完成
    public function complete()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['sname']  = Session::get('studentname');
            $where['status'] = 2;
            $list            = ordermodel::where($where)->select();
            $count           = sizeof($list);

            $where1['receive'] = Session::get('studentname');
            $where1['sstatus'] = 1;
            $slist             = notifymodel::where($where1)->select();
            $ccount            = sizeof($slist);

            $this->assign([
                'count'  => $count,
                'xcount' => $ccount
            ]);

            return $this->fetch("student/complete");
        }
    }

    public function know()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $res              = expertmodel::select();
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign([
                'expert' => $res,
                'count'  => $count
            ]);
            return $this->fetch("student/know");
        }

    }



    //预约状态与当前时间检测函数
    //存在问题 怎么让他 自动执行
    public function operastatus()
    {
        $status = 0;

        //系统时间2020-08-29 08:41:06
        //预约时间2020-08-25 18:12:04
        //获取当前时间

        // date_default_timezone_set("Asia/Shanghai");
        $time = date('Y-m-d H:i:s', time());
        //var_dump($time);
        $res = ordermodel::select();
        foreach ($res as $order) {
            if (strtotime($order['time']) < strtotime($time)) {
                ordermodel::where('id', $order['id'])->update(['status' => 2]);
                $status = 1;
            } else if (strtotime($order['time']) == strtotime($time)) {
                ordermodel::where('id', $order['id'])->update(['status' => 2]);
                $status = 1;
            } else {
                ordermodel::where('id', $order['id'])->update(['status' => 1]);
                $status = 1;
            }
        }
        return $time;
    }


    //预约
    //   新增消息通知
    public function order()
    {

        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $info    = studentmodel::where('id', Session::get('studentid'))->find();
            $slevel  = selectmodel::field('slevel')->select();
            $room    = selectmodel::field('roomnum')->select();
            $ename   = expertmodel::field('name')->select();
            $id      = $info['id'];
            $nation  = $info['nation'];
            $sex     = $info['sex'];
            $depart  = $info['depart'];
            $mail    = $info['mail'];
            $age     = $info['age'];
            $address = $info['address'];
            $phone   = $info['phone'];
            $list    = selectmodel::field('sex')->select();
            $list2   = selectmodel::field('depart')->select();


            $where1['receive'] = Session::get('studentname');
            $where1['sstatus'] = 1;
            $slist             = notifymodel::where($where1)->select();
            $ccount            = sizeof($slist);

            $this->assign([
                'id'          => $id,
                'name'        => Session::get('studentname'),
                'sex'         => $sex,
                'mail'        => $mail,
                'depart'      => $depart,
                'age'         => $age,
                'address'     => $address,
                'phone'       => $phone,
                'nation'      => $nation,
                'slevellist'  => $slevel,
                'roomnumlist' => $room,
                'enamelist'   => $ename,
                'info'        => $info,
                'sexlist'     => $list,
                'departlist'  => $list2,
                'count'       => $ccount

            ]);
            return $this->fetch("student/order");
        }

    }

    //显示修改手机信息页面
    public function phone()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $info              = studentmodel::where('id', Session::get('studentid'))->find();
            $phone             = $info['phone'];
            $where1['receive'] = Session::get('studentname');
            $where1['sstatus'] = 1;
            $slist             = notifymodel::where($where1)->select();
            $ccount            = sizeof($slist);
            $this->assign([
                'phone' => $phone,
                'count' => $ccount
            ]);
            return $this->fetch("student/phone");
        }
    }

    //修改手机操作
    public function updatephone($phone)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $newdata = [
                    'id'    => Session::get('studentid'),
                    'phone' => $phone,
                ];
                $res     = studentmodel::update($newdata);
                if ($res) {
                    $this->success('', '', '1');
                } else {
                    $this->error('', '', '0');
                }
            }
        }
    }

    //获取更新后的信息
    public function getinfo($id)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $list         = selectmodel::field('sex')->select();
            $list2        = selectmodel::field('depart')->select();
            $list3        = studentmodel::where('id', '=', $id)->find();
            $arr          = array();
            $arr['list']  = $list;
            $arr['list2'] = $list2;
            $arr['list3'] = $list3;
            return json($arr);
        }
    }

    //个人信息
    public function info()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $info             = studentmodel::where('id', Session::get('studentid'))->find();
            $photo            = $info['photo'];
            $id               = $info['id'];
            $nation           = $info['nation'];
            $sex              = $info['sex'];
            $depart           = $info['depart'];
            $mail             = $info['mail'];
            $age              = $info['age'];
            $address          = $info['address'];
            $phone            = $info['phone'];
            $list             = selectmodel::field('sex')->select();
            $list2            = selectmodel::field('depart')->select();
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list1            = notifymodel::where($where)->select();
            $count            = sizeof($list1);
            $this->assign([
                'photo'      => $photo,
                'id'         => $id,
                'name'       => Session::get('studentname'),
                'sex'        => $sex,
                'mail'       => $mail,
                'depart'     => $depart,
                'age'        => $age,
                'address'    => $address,
                'phone'      => $phone,
                'nation'     => $nation,
                'info'       => $info,
                'sexlist'    => $list,
                'departlist' => $list2,
                'count'      => $count
            ]);
            return $this->fetch("student/info");
        }

    }

//修改信息发送验证码

    public function sms($tel)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $param                 = rand(1000, 9999);
            $options['accountsid'] = 'd1e5040273e564e43bc4289d756cbd0f';
            $options['token']      = 'd8b817ed9afe00f1feb9b8a99959cf13';
            $ucpaas                = new Ucpaas($options);

            $appid      = "1afae4daf8df4e7ebba63d9c7c988dbe";
            $templateid = "565858";
            $mobile     = $tel;
            $uid        = $param;
            $result     = $ucpaas->SendSms($appid, $templateid, $param, $mobile, $uid);
            $arr        = array();
            //$arr['result'] = $result;
            $arr['param'] = $param;
            return $result;
        }
    }


    //修改
    public function update()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $data = Request::post();
                //salt化
                $password = $data['pwd'];
                $mpwd     = md5($password);
                $mspwd    = $this->salt($password);  //执行MD5散列
                $newdata  = [
                    'id'      => $data['id'],
                    'name'    => $data['name'],
                    'pwd'     => $mpwd,
                    'salt'    => $mspwd,
                    'sex'     => $data['sex'],
                    'mail'    => $data['mail'],
                    'age'     => $data['age'],
                    'depart'  => $data['depart'],
                    'address' => $data['address'],
                    'phone'   => $data['phone'],
                    'nation'  => $data['nation'],
                ];
                $res      = studentmodel::update($newdata);
                if ($res) {
                    $this->success('', '', '1');
                } else {
                    $this->error('', '', '0');
                }
            }
        }
    }


    //图片上传
    public function photo()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $file  = $this->request->file('file');
            $info  = $file->move('../public/photo');
            $spath = studentmodel::where('id', Session::get('studentid'))->value('photo');
//        $explodepath = array();
//        $explodepath = explode("\\", $spath);
//        $rootpath    = Env::get('ROOT_PATH');
//        $oldpath     = $rootpath . 'public\photo\\' . $explodepath[3] . '\\' . $explodepath[4];
            if ($info) {
                $path    = '..\..\photo\\' . $info->getSaveName();
                $newdata = [
                    'id'    => Session::get('studentid'),
                    'photo' => $path,
                ];
                $res     = studentmodel::update($newdata);
//            unlink($oldpath);
                if ($res) {
                    return json(array('code' => 1, 'msg' => '成功', 'src' => $path));
                } else {
                    return json(array('code' => 0, 'msg' => '失败', 'src' => $path));
                }
            } else {
                return json(array('code' => 0, 'msg' => '失败', 'src' => '错误'));
            }
        }

    }

    //若头像为空则设置默认图片
    public function checkphoto()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $spath = studentmodel::where('id', Session::get('studentid'))->value('photo');
            if (empty($spath)) {
                $path    = '..\..\photo\default.jpg';
                $newdata = [
                    'id'    => Session::get('studentid'),
                    'photo' => $path,
                ];
                $res     = studentmodel::update($newdata);
                if ($res) {
                    $this->success('', '', '1');
                } else {
                    $this->error('', '', '0');
                }
            } else {
                $this->error('', '', '0');
            }
        }
    }

    //留言
    public function meaasgeinsert()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $data    = Request::post();
                $content = $data['content'];
                //当前系统时间
                $time    = date('Y-m-d h:i:s', time());
                $newdata = [
                    'content' => $data['content'],
                    'time'    => $time,
                    'towho'   => $data['towho']
                ];
                if ($data['towho'] != 'admin') {
                    $notifydata = [
                        "write"     => Session::get('studentname'),
                        "receive"   => $data['towho'],
                        "estatus"   => 1,
                        "opera"     => 3,
                        "time"      => $time,
                        "ordertime" => $time
                    ];
                    $notify     = notifymodel::insert($notifydata);
                }

                $res = messagemodel::insert($newdata);
                if ($res) {
                    $this->success('', '', '1');
                } else {
                    $this->error('', '', '0');
                }
            }
        }
    }


    //显示进行中的预约
    public function orderjson()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $page            = input("get.page") ? input("get.page") : 1;
            $page            = intval($page);
            $limit           = input("get.limit") ? input("get.limit") : 1;
            $limit           = intval($limit);
            $start           = $limit * ($page - 1);
            $where['sname']  = Session::get('studentname');
            $where['status'] = 1;
            $list            = ordermodel::limit($start, $limit)->where($where)->select();
            $count           = sizeof($list);
            $data["total"]   = $count;
            $data["msg"]     = "";
            $data["code"]    = 0;
            $data["data"]    = $list;
            $rs              = json($data);
            return $rs;
        }
    }


    //显示已完成的预约
    public function completejson()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $page            = input("get.page") ? input("get.page") : 1;
            $page            = intval($page);
            $limit           = input("get.limit") ? input("get.limit") : 1;
            $limit           = intval($limit);
            $start           = $limit * ($page - 1);
            $where['sname']  = Session::get('studentname');
            $where['status'] = 2;
            $list            = ordermodel::where($where)->limit($start, $limit)->select();
            $count           = sizeof($list);
            $data["total"]   = $count;
            $data["msg"]     = "";
            $data["code"]    = 0;
            $data["data"]    = $list;
            $rs              = json($data);
            return $rs;
        }
    }


    //已完成删除
    public function completedel($id)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $del = ordermodel::destroy($id);
            if ($del) {
                $this->success('删除成功', '', '1');
            } else {
                $this->error('删除失败', '', '0');
            }
        }
    }


    //新增预约
    //消息提醒
    public function insert()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $status = 0;
                $data   = Request::post();
                $time   = date('Y-m-d H:i:s', time());
                if (strtotime($data['time']) < strtotime($time)) {
                    $status = 3;
                } else if (strtotime($data['time']) == strtotime($time)) {
                    $status = 4;
                } else {
                    $newdata = [
                        "sid"     => $data['id'],
                        "sname"   => $data['sname'],
                        "age"     => $data['age'],
                        "sex"     => $data['sex'],
                        "depart"  => $data['depart'],
                        "slevel"  => $data['slevel'],
                        "nation"  => $data['nation'],
                        "phone"   => $data['phone'],
                        "time"    => $data['time'],
                        "address" => $data['address'],
                        "ename"   => $data['ename'],
                        "problem" => $data['content'],
                        "status"  => 1
                    ];

                    $notifydata = [
                        "write"     => Session::get('studentname'),
                        "receive"   => $data['ename'],
                        "estatus"   => 1,
                        "opera"     => 1,
                        "time"      => $time,
                        "ordertime" => $data['time']
                    ];
                    $notify     = notifymodel::insert($notifydata);

                    $res = ordermodel::insert($newdata);
                    if ($res) {
                        $status = 1;
                    } else {
                        $status = 2;
                    }
                }
            }
        }
        return $status;
    }

    //取消预约
    public function cancel()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $status     = 0;
                $data       = Request::post();
                $time       = date('Y-m-d H:i:s', time());
                $res        = ordermodel::destroy($data['id']);
                $notifydata = [
                    "write"     => Session::get('studentname'),
                    "receive"   => $data['ename'],
                    "estatus"   => 1,
                    "opera"     => 2,
                    "time"      => $time,
                    "ordertime" => $data['time']
                ];
                $notify     = notifymodel::insert($notifydata);
                if ($res) {
                    $status = 1;
                } else {
                    $status = 2;
                }
            }
        }
        return $status;
    }


    //检查消息通知
    public function checkmessage()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            return $count;
        }

    }

    //取消提示
    public function changenotify()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $status           = 0;
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list             = notifymodel::where($where)->select();
            foreach ($list as $notify) {
                notifymodel::where('id', $notify['id'])->update(['sstatus' => 2]);
                $status = 1;
            }

        }
        return $status;
    }


    //显示预约信息
    public function geteditorder($id)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            //显示要修改的信息
            $list         = selectmodel::field('sex')->select();
            $list2        = selectmodel::field('depart')->select();
            $list3        = selectmodel::field('slevel')->select();
            $list4        = ordermodel::where('id', '=', $id)->find();
            $list5        = expertmodel::field('name')->select();
            $arr          = array();
            $arr['list']  = $list;
            $arr['list2'] = $list2;
            $arr['list3'] = $list3;
            $arr['list4'] = $list4;
            $arr['list5'] = $list5;
            return json($arr);
        }
    }

    //通知模板
    public function notify()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $res = notifymodel::where('receive', Session::get('studentname'))->select();
            $this->assign([
                'notify' => $res,
            ]);
            return $this->fetch('student/notify');
        }
    }

    //清除通知
    public function clear()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $status           = 0;
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 2;
            $list             = notifymodel::where($where)->select();
            foreach ($list as $notify) {
                notifymodel::destroy($notify['id']);
                //notifymodel::where('id', $notify['id'])->update(['estatus' => 3]);
                $status = 1;
            }

        }
        return $status;
    }

    //关于我们
    public function aboutus()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("student/aboutus");
        }
    }


    //隐私条款
    public function yinsi()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("student/yinsi");
        }
    }


    //关于我们
    public function feedback()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("student/feedback");
        }
    }


    //关于我们
    public function contactus()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('studentname');
            $where['sstatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("student/contactus");
        }
    }


    //退出
    function logout()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            Session::delete('studentname');
            Session::delete('studentid');
            return $this->fetch('login/login');
        }
    }
}